//
//  caigouxinxi.m
//  BAOASystem
//
//  Created by michael on 2018/3/7.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "caigouxqViewController.h"
#import "caigouxinxi.h"
#import "productCollectionViewCell.h"

@implementation caigouxinxi

- (void)setString:(NSString *)string{


//NSLog(@"11%@",id);
//_string = string;
// self.currentPage = 1;
// [self initview];
//[self.CollectionView reloadData]; //重新换数据源的时候 记得重回用的cell上的

 
}
- (void)setDict:(NSDictionary *)dict{


//NSLog(@"11%@",id);
_dict = dict;
 self.currentPage = 1;
 [self initview];
[self.CollectionView reloadData]; //重新换数据源的时候 记得重回用的cell上的

 
}
- (void)initview{

 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
     layout.itemSize = CGSizeMake((KScreenWidth)/2,150);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
     layout.minimumLineSpacing = 0; //设置行间距
layout.minimumInteritemSpacing = 0; //设置列间距
  _CollectionView =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) collectionViewLayout:layout];
  _CollectionView.backgroundColor = [UIColor whiteColor];
          layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _CollectionView.dataSource = self;
        //设置代理,监听
        _CollectionView.delegate = self;
            self.CollectionView.alwaysBounceVertical = YES;
//[_CollectionView registerNib:[UINib nibWithNibName:@"productCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"productCollectionViewCell"];
    [_CollectionView registerClass:[qiyecaigouxqCollectionViewCell class] forCellWithReuseIdentifier:@"productCollectionViewCell"];

[self addSubview:_CollectionView];
 __weak typeof(self) weakSelf = self;

    [self.CollectionView addHeaderWithCallback:^{
            weakSelf.currentPage = 1;
        [weakSelf loadData]; //加载网络数据

    
    }];
    [self.CollectionView headerBeginRefreshing];
    [self.CollectionView addFooterWithCallback:^{
//        if ((self.currentPage - 1)*KPageSize < self.totalCounts ){
          if (weakSelf.lastPage==0 ){

  
     [weakSelf loadMore];
 
        }else{
            
            [weakSelf.CollectionView footerEndRefreshing];
            [weakSelf makeToast:NSLocalizedString(@"没有更多了", nil)];

        }
        
    }];

}
- (void)loadData{

    NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
//  dics[@"cpid"] = _string;
   [dics setValue:_dict[@"pdid"] forKey:@"cpid"];
   [dics setValue:_dict[@"searchstr"] forKey:@"pdname"];
//  dics[@"pdname"] = _dict[@"seachstr"];
  dics[@"pageNumber"] = @"1";
  dics[@"pageSize"] = @"20";
       dics[@"cpstatus"] = @"1";

   self.currentPage = 1;

  NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/company/pdmlist?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  if (self.currentPage ==1)
        {
            [self.dataArray removeAllObjects];
        }
  NSLog(@"%@",dictionary);
  _lastPage = [dictionary[@"lastPage"]intValue];
  self.totalCounts =[dictionary[@"totalRow"] intValue];

  _dataArray = [NSMutableArray array];
  NSDictionary *dic = dictionary[@"pdpage"];
   NSArray *array = dic[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
[_CollectionView reloadData];
        self.currentPage++;

        [self.CollectionView headerEndRefreshing];
 }andFailure:^{
         [self.CollectionView headerEndRefreshing];

//         [self.tableView headerEndRefreshing];

 }];

}
- (void)loadMore{

    NSMutableDictionary *dics=[NSMutableDictionary dictionary];
      dics[@"userName"] = @"administrator";
    dics[@"password"] = @"b628e2c3b78ef9bf269c489b35176e48";
    
    dics[@"language"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
     dics[@"website"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"website"];
        dics[@"type"] = BAtype;
        dics[@"cpstatus"] = @"1";

  dics[@"pageNumber"] = [NSString stringWithFormat:@"%d",_currentPage];
  dics[@"pageSize"] = @"20";
   [dics setValue:_dict[@"pdid"] forKey:@"cpid"];
   [dics setValue:_dict[@"searchstr"] forKey:@"pdname"];
 
  NSString *dicstring = [httpTool dictionaryToJson:dics];
dicstring= [dicstring URLEncodedString];
NSString *url = [[NSString alloc]initWithFormat:@"%@/api/company/pdmlist?data=%@",BAip,dicstring];
 [httpTool ZBPostNetDataWith:url withDic:nil andSuccess:^(NSDictionary* dictionary){
  NSLog(@"%@",dictionary);
    NSDictionary *dic = dictionary[@"pdpage"];

 NSArray *array = dic[@"list"];
for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
_lastPage = [dictionary[@"lastPage"]intValue];
  self.totalCounts =[dictionary[@"totalRow"] intValue];

       self.currentPage++;
        [self.CollectionView footerEndRefreshing];
[_CollectionView reloadData];
 }andFailure:^{
         [self.CollectionView footerEndRefreshing];

//         [self.tableView headerEndRefreshing];

 }];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


 static NSString* cellId0 = @"productCollectionViewCell";
    qiyecaigouxqCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId0 forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];

   cell.dic =_dataArray [indexPath.row];
   [cell layoutSubviews];
    return cell;

   
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSArray *list = _dic[@"ctgpdlist"];
NSLog(@"111");
NSDictionary *dic =_dataArray [indexPath.row];
NSString *pdid =dic[@"id"];
caigouxqViewController *vc = [[caigouxqViewController alloc]init];

//shangpinxqViewController *vc = [[shangpinxqViewController alloc]init];
vc.pdid = pdid;
vc.hidesBottomBarWhenPushed = YES;
[self.ViewController.navigationController pushViewController:vc animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSArray *list = _dic[@"ctgpdlist"];

    return _dataArray.count;

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

